<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Think\Exception;
use Think\Model;

class ServiceController extends AdminbaseController{
    
	protected $serviceModel;
	
	public function _initialize() {
		parent::_initialize();
		$this->serviceModel = M("Service");
	}

    //服务列表
    public function index(){
        $services=$this->serviceModel->where(['is_deleted'=>0])->select();
        foreach ($services as &$service){
            if($service['pid']!=0){
                foreach ($services as $v){
                    if($v['id']==$service['pid']){
                        $service['pname']=$v['name'];
                    }
                }
            }else{
                $service['pname']='';
            }
        }
        $this->assign("services",$services);
        $this->display();
	}

    //编辑
    public function edit(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('缺少参数');
        }
        $service=$this->serviceModel->where(['id'=>$id,'is_deleted'=>0])->find();
        if($service){
            if(IS_POST){
                $data['pid']=I('pid',0,'intval');
                $data['name']=I('name');
                if(!$data['pid']||!$data['name']){
                    $this->error('名称必须');
                }
                $p=$this->serviceModel->where(['id'=>$data['pid']])->find();
                if(!$p){
                    $this->error('数据错误');
                }
                if($p['has_cats']!=1){
                    $this->error('该父分类不能有子分类');
                }
                $result=$this->serviceModel->where(['id'=>$id])->save($data);
                if($result!==false){
                    $this->success('编辑成功',U('Service/index'));
                }else{
                    $this->error('编辑失败');
                }
            }else{
                $pservices=$this->serviceModel->where(['is_deleted'=>0,'has_cats'=>1])->select();
                $this->assign("service",$service);
                $this->assign("pservices",$pservices);
                $this->display();
            }
        }else{
            $this->error('服务不存在');
        }
    }

    //添加
    public function add(){
        if(IS_POST){
            $pid=I('pid',0,'intval');
            $data['name']=I('name');
            if(!$pid||!$data['name']){
                $this->error('名称必须');
            }
            $p=$this->serviceModel->where(['id'=>$pid])->find();
            if(!$p){
                $this->error('数据错误');
            }
            if($p['has_cats']!=1){
                $this->error('该父分类不能有子分类');
            }
            $data['pid']=$pid;
            $data['create_time']=time();
            $result=$this->serviceModel->add($data);
            if($result!==false){
                $this->success('添加成功',U('Service/index'));
            }else{
                $this->error('添加失败');
            }
        }else{
            $pservices=$this->serviceModel->where(['is_deleted'=>0,'has_cats'=>1])->select();
            $this->assign("pservices",$pservices);
            $this->display();
        }
    }

    //删除
    public function delete(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('缺少参数');
        }
        $service=$this->serviceModel->where(['id'=>$id,'is_deleted'=>0])->find();
        if($service){
            if($service['pid']==0||$service['is_other']==1){
                $this->error('不能删除该分类');
            }
            //开启事物
            M('Service')->startTrans();
            try {
                //删除分类
                $result = $this->serviceModel->where(['id'=>$id])->save(['is_deleted'=>1]);
                if ($result === false) {
                    throw new Exception('删除分类失败！');
                }
                //查找该分类同级的其他分类项
                $other=$this->serviceModel->where(['pid'=>$service['pid'],'is_other'=>1])->find();
                if($other){
                    //更改分类下项目为其他分类
                    $result = M('BusinessService')->where(['service_id' => $id])->save(['service_id' => $other['id']]);
                    if ($result === false) {
                        throw new Exception('更改分类失败！');
                    }
                }else{
                    throw new Exception('缺少其他分类！');
                }
                M('Service')->commit();
                $this->success('删除成功');
            } catch (Exception $e) {
                M('Service')->rollback();
                $this->error('删除失败');
            }
        }else{
            $this->error('服务不存在');
        }
    }
}